<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/19
 * Time: 21:58
 */

namespace app\api\model;

class Product extends BaseModel
{
    protected $hidden = ['delete_time', 'update_time', 'create_time', 'pivot'];
    //关联productImg表
    public function productImg()
    {
        return $this -> hasMany('ProductImage', 'product_id', 'id');
    }
    //关联productProperty表
    public function productProperty()
    {
        return $this -> hasMany('ProductProperty', 'product_id', 'id');
    }
    //获取图片地址前缀
    public function getMainImgUrlAttr($value, $data)
    {
        return $this -> getImgPrefix($value, $data);
    }
    //获取最新商品
    public function getMostRecent($count)
    {
        return $this -> limit($count) -> order('create_time desc') -> select();
    }
    //获取分类下的商品
    public function getCategoryProducts($id)
    {
        return self::where(['category_id' => $id]) -> select();
    }
    //获取一个商品的祥细信息
    public static function getOneProduct($id = '')
    {
        return self::with(
            [
                'productProperty',
                'productImg' => function($query) {
                    $query -> with('imgs') ->order(['order' => 'asc']);
                 },
            ]
        ) -> find($id);
    }
}